package com.yiren.dbaa.module.project_edit.view.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.db.dao.bean.SynExamBean;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.model.CheckBoxCommonModel;
import com.yiren.dbaa.currency.model.EdittextCommonModel;
import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lwb
 * on 2021/11/2
 * Describe  灯光参数设置Fragment
 */
public class LightParamFragment extends Fragment {

    @BindView(R.id.cb_start_engine)
    CheckBox cbStartEngine;
    @BindView(R.id.et_start_engine_voice)
    EditText etStartEngineVoice;
    @BindView(R.id.et_no_engine_times)
    EditText etNoEngineTimes;
    @BindView(R.id.cb_close_all_light)
    CheckBox cbCloseAllLight;
    @BindView(R.id.et_close_light_voice)
    EditText etCloseLightVoice;
    @BindView(R.id.et_no_light_times)
    EditText etNoLightTimes;
    @BindView(R.id.cb_advance_voice)
    CheckBox cbAdvanceVoice;
    @BindView(R.id.cb_right_next)
    CheckBox cbRightNext;
    @BindView(R.id.ll_right_next_time)
    LinearLayout llRightNextTime;
    @BindView(R.id.et_right_next_time)
    EditText etRightNextTime;
    @BindView(R.id.cb_error_next)
    CheckBox cbErrorNext;
    @BindView(R.id.cb_right_no_operate)
    CheckBox cbRightNoOperate;

    private SiteEntity site;
    /*** 存放参数值改变的临时map */
    private Map<String, Object> tempMap;
    private boolean isPartner;
    private SynExamBean synExamBean;
    private Gson gs = new Gson();
    private List<EdittextCommonModel> edittextCommonModels;
    private List<CheckBoxCommonModel> checkBoxCommonModels;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_light_param, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        setEditText();
        setCheckBox();
    }

    private void initData() {
        site = (SiteEntity) getArguments().getSerializable("site");
        isPartner = LoadConfigApi.loadUserParamConfig(getActivity()).isPartner();
        tempMap = new HashMap<>();
        synExamBean = gs.fromJson(isPartner ? site.getPartneModifyVal() : site.getCoachModifyVal(), new TypeToken<SynExamBean>() {
        }.getType());
        if (null == synExamBean) synExamBean = new SynExamBean();
        llRightNextTime.setVisibility((boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D7.toString()) ? View.GONE : View.VISIBLE);
    }

    private void setEditText() {
        edittextCommonModels = new ArrayList<>();
        edittextCommonModels.add(new EdittextCommonModel(etStartEngineVoice, SiteConfigCodeEnum.D2.toString()));
        edittextCommonModels.add(new EdittextCommonModel(etNoEngineTimes, SiteConfigCodeEnum.D3.toString()));
        edittextCommonModels.add(new EdittextCommonModel(etCloseLightVoice, SiteConfigCodeEnum.D5.toString()));
        edittextCommonModels.add(new EdittextCommonModel(etNoLightTimes, SiteConfigCodeEnum.D6.toString()));
        edittextCommonModels.add(new EdittextCommonModel(etRightNextTime, SiteConfigCodeEnum.D8.toString()));
        for (EdittextCommonModel model : edittextCommonModels) {
            model.getEt().setText(model.getInitValue());
            model.getEt().addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    SignalGConstant.examConfigMap.put(model.getKey(), s.toString());
                    if (!s.toString().equals(model.getInitValue())) {
                        tempMap.put(model.getKey(), s.toString());
                    } else {
                        tempMap.remove(model.getKey());
                    }
                }
            });
        }
    }

    private void setCheckBox() {
        checkBoxCommonModels = new ArrayList<>();
        checkBoxCommonModels.add(new CheckBoxCommonModel(cbStartEngine, SiteConfigCodeEnum.D1.toString()));
        checkBoxCommonModels.add(new CheckBoxCommonModel(cbCloseAllLight, SiteConfigCodeEnum.D4.toString()));
        checkBoxCommonModels.add(new CheckBoxCommonModel(cbAdvanceVoice, SiteConfigCodeEnum.D9.toString()));
        checkBoxCommonModels.add(new CheckBoxCommonModel(cbRightNext, SiteConfigCodeEnum.D7.toString()));
        checkBoxCommonModels.add(new CheckBoxCommonModel(cbErrorNext, SiteConfigCodeEnum.D10.toString()));
        checkBoxCommonModels.add(new CheckBoxCommonModel(cbRightNoOperate, SiteConfigCodeEnum.D11.toString()));
        for (CheckBoxCommonModel model : checkBoxCommonModels) {
            model.getCb().setChecked(model.isInitValue());
            model.getCb().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    SignalGConstant.examConfigMap.put(model.getKey(), isChecked);
                    if (isChecked != model.isInitValue()) {
                        tempMap.put(model.getKey(), isChecked);
                    } else {
                        tempMap.remove(model.getKey());
                    }
                    if (model.getKey().equals(SiteConfigCodeEnum.D7.toString())) {
                        llRightNextTime.setVisibility(isChecked ? View.GONE : View.VISIBLE);
                    }
                }
            });
        }
    }

    @OnClick(R.id.tv_save_param)
    public void onViewClicked() {
        if (tempMap.size() == 0) return;
        for (String key : tempMap.keySet()) {
            synExamBean.getExamConfigMap().put(key, tempMap.get(key));
        }
        if (isPartner) {
            site.setPartneModifyVal(gs.toJson(synExamBean));
        } else {
            site.setCoachModifyVal(gs.toJson(synExamBean));
        }
        UserDbController.getInstance(false).update(site);
        MessageBox.show(getActivity(), "保存成功!");
    }
}